<?php
namespace structure\components;

class Date extends \components\Component
{
	public function Start()
	{
		date_default_timezone_set('Europe/Kiev');

		$pattern = $this->GetAttribute('pattern');

		$hourFormat = $this->GetAttribute('hourFormat');

		$pattern = preg_replace("~(?<![\w])YYYY(?![\w])~", strftime("%Y"), $pattern);
		$pattern = preg_replace("~(?<![\w])YY(?![\w])~", strftime("%y"), $pattern);
		$pattern = preg_replace("~(?<![\w])MM(?![\w])~", strftime("%m"), $pattern);
		$pattern = preg_replace("~(?<![\w])M(?![\w])~", (int)strftime("%m"), $pattern);
		$pattern = preg_replace("~(?<![\w])DD(?![\w])~", strftime("%d"), $pattern);
		$pattern = preg_replace("~(?<![\w])D(?![\w])~", (int)strftime("%d"), $pattern);

		$pattern = preg_replace("~(?<![\w])hh(?![\w])~", strftime($hourFormat == 12 ? "%I" : "%H"), $pattern);
		$pattern = preg_replace("~(?<![\w])h(?![\w])~", (int)strftime($hourFormat == 12 ? "%I" : "%H"), $pattern);
		$pattern = preg_replace("~(?<![\w])mm(?![\w])~", strftime("%M"), $pattern);
		$pattern = preg_replace("~(?<![\w])m(?![\w])~", (int)strftime("%M"), $pattern);
		$pattern = preg_replace("~(?<![\w])ss(?![\w])~", strftime("%S"), $pattern);
		$pattern = preg_replace("~(?<![\w])s(?![\w])~", (int)strftime("%S"), $pattern);

		return $pattern;
	}
}